package org.shan.code.generator;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {

    // 数据库配置信息
    public static final String URL = "jdbc:mysql://123.60.177.231:3306/smart_make";
    public static final String NAME = "smart_make";
    public static final String PASS = "WYeWMYeDZArJ8nGn";
    public static final String DRIVER = "com.mysql.jdbc.Driver";


    public static String basePath = "E:\\IdeaProjects\\smartfactory";

    public static void main(String[] args) throws Exception {

        List<String> tableNames = CodeGenerator.searchTableNames("smart_make");
        System.out.println(tableNames);

        String[] strings = tableNames.toArray(new String[0]);
        strings = new String[]{"t_device_data"};

        // 生成 entity
        EntityGenerator.basePath = basePath + "\\api\\api_make_win\\smart_make_device_api";
        EntityGenerator.URL = URL;
        EntityGenerator.NAME = NAME;
        EntityGenerator.PASS = PASS;
        EntityGenerator entityGenerator = new EntityGenerator();
        entityGenerator.generateTables = strings;
        try {
            entityGenerator.generate();
            System.out.println("generate Entity to classes successful!");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 生成 mapper
        MapperGenerator mapperGenerator = new MapperGenerator();
        mapperGenerator.generateTables = strings;
        mapperGenerator.basePath = basePath + "\\service\\make_win_service\\smart_make_device_service"; // 指定生成的位置,默认是当前工程
        try {
            mapperGenerator.generate();
            System.out.println("mapper Entity to classes successful!");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 生成 service
        ServiceGenerator serviceGenerator = new ServiceGenerator();
        serviceGenerator.generateTables = strings;
        serviceGenerator.interfacePath = basePath + "\\api\\api_make_win\\smart_make_device_api";
        serviceGenerator.realPath = basePath + "\\service\\make_win_service\\smart_make_device_service"; // 指定生成的位置,默认是当前工程;
        try {
            serviceGenerator.generate();
            System.out.println("service to classes successful!");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static List<String> searchTableNames(String dateBaseName) throws Exception {
        Connection conn;
        PreparedStatement pStemt = null;
        Class.forName(DRIVER);
        conn = DriverManager.getConnection(URL, NAME, PASS);
        if (conn != null) {
            System.out.println("数据库连接成功");
        }

        List<String> list = new ArrayList<String>();

        String sql = String.format("select table_name from information_schema.tables where table_schema='%s'", dateBaseName);
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery(sql);
        while (result.next()) {
            list.add(result.getString("TABLE_NAME"));
        }
        return list;
    }
}
